package com.eastfair.imaster.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.eastfair.boot.request.PageParams;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.core.utils.StringUtils;
import com.eastfair.imaster.dao.ConferenceUserMapper;
import com.eastfair.imaster.dto.ConferenceUserPageQuery;
import com.eastfair.imaster.entity.ConferenceUser;
import com.eastfair.imaster.service.ConferenceUserService;
import com.eastfair.imaster.vo.ConferenceUserVO;
import com.eastfair.projectcore.api.ProjectCoreServiceFeign;
import com.eastfair.core.snowflake.DefaultUidGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 业务实现类
 * 论坛报名人员
 * </p>
 *
 * @author zjn
 * @date 2021-08-31
 */
@Slf4j
@Service
@DS("#thread.tenant")
public class ConferenceUserServiceImpl extends SuperServiceImpl<ConferenceUserMapper, ConferenceUser> implements ConferenceUserService {

    @Autowired
    ProjectCoreServiceFeign projectCoreServiceFeign;

    /**************sharding 分库分表操作 *****************/
    @Override
    public boolean updateByIdOfShard(ConferenceUser entity) {
        UpdateWrapper<ConferenceUser> updateWrapper = new UpdateWrapper<>();
        //可将指定字段更新为null
        updateWrapper.lambda().eq(ConferenceUser::getProjectId, entity.getProjectId()).eq(ConferenceUser::getId, entity.getId());
        entity.setProjectId(null);
        return SqlHelper.retBool(this.getBaseMapper().update(entity, updateWrapper));
    }

    /**
     * 处理新增相关处理
     *
     * @param model 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerSave(ConferenceUser model) {
        //雪花ID
        model.setId(DefaultUidGenerator.getUidOfProjectId(ContextUtil.getProjectId()));
        model.setProjectId(ContextUtil.getProjectId());
        return R.successDef();
    }

    @Override
    public Page<ConferenceUserVO> getPageList(PageParams<ConferenceUserPageQuery> params) {
        if (params.getModel().getConferenceId() == null) {
            throw new NullPointerException("error params, conferenceId is null");
        }

        QueryWrapper<ConferenceUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ConferenceUser::getProjectId, ContextUtil.getProjectId()).eq(ConferenceUser::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(ConferenceUser::getIsEnabled, BusinessConstant.ENABLE_YES);
        if (!StringUtils.isBlank(params.getModel().getName())) {
            queryWrapper.lambda().like(ConferenceUser::getName, params.getModel().getName());
        }

        return ConvertUtil.convertPage(page(params.buildPage(), queryWrapper), ConferenceUserVO.class);
    }
}
